<?php

/* 商品属性 goodsattr */
class GoodsattrModel extends BaseModel
{
    var $table  = 'goods_attr';
    var $prikey = 'gattr_id';
    var $_name  = 'goodsattr';

    var $_relation  = array(
        // 一个商品属性只能属于一个商品
        'belongs_to_goods' => array(
            'model'         => 'goods',
            'type'          => BELONGS_TO,
            'foreign_key'   => 'goods_id',
            'reverse'       => 'has_goodsattr',
        ),
    );
	
	//根据goods_id获取gattr_id最小和最大的信息
	//goods_id:商品id
	function get_infos($goods_id){
		if($goods_id>0){
			$sql="select * from ".$this->table." where goods_id=".$goods_id;
			$row = $this->db->getAll($sql);
			return $row;
		}
	}
	function delete($goods_id){
		if($goods_id>0){
			$sql="delete from ".$this->table." where goods_id=".$goods_id;
			$row = $this->db->query($sql);
			return $row;
		}
	}
    
    /**
     *    更新某个商品的关联属性
     *
     *    @author    yanghj 2014.7.17
     *    @param array   $arr      要更新的数组数据
     *    @param intval  $goods_id 商品ID
     *    @param str     $act      add 为添加 edit 为修改 
     *    @return
     */
    function update_data($arr, $goods_id, $act = 'add'){
		if($goods_id>0 && is_array($arr)){
            if($act == 'add')
            {
                sort($arr);
                $rs = parent::add($arr);               
            }
            if($act == 'edit')
            {
                $sql="SELECT gattr_id,attr_id,attr_value,goods_id from ".$this->table." where goods_id=".$goods_id;
                $row = $this->getAll($sql);
                if($row)
                {
                    foreach($arr as $key=>$val)
                    {
                        $attr_ids[$key] =$val['attr_id'];
                        $attr_values[$key] =$val['attr_value'];   
                    }
                    foreach($row as  $key => $val)
                    {
                        $att_data[$val['attr_id']][$val['attr_value']]['gattr_id'] = $val['gattr_id'];
                    }                    
                    foreach($attr_ids as $key => $attr_id)
                    {
                        if(isset($att_data[$attr_id][$attr_values[$key]]))
                        {
                            $gattr_ids .= $att_data[$attr_id][$attr_values[$key]]['gattr_id'].',';
                            unset($arr[$key]);
                        }
                    }
                    if($gattr_ids)
                    {
                       $gattr_ids = substr($gattr_ids,0,-1);
                       $sql="delete from ".$this->table." where goods_id=".$goods_id." and gattr_id not in(".$gattr_ids.")" ;
			           $this->db->query($sql); 
                    }
                    else
                    {
                        $sql="delete from ".$this->table." where goods_id=".$goods_id;
			            $this->db->query($sql);
                    }                   
                }
                if($arr)
                {
                   sort($arr);
                   $rs = parent::add($arr); 
                }
           }			
		}
        return $rs;
	}
}

?>